package com.atguigu.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.utils.MykafkaUtil;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.text.SimpleDateFormat;

/**
 *
 */
public class UniqueVisitApp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        // TODO: 从kafka中读取数据
//        3.1 声明消费者主题以及消费者组
        String topic = "dwd_page_log";
        String groupId = "unique_visit_app_group";
        String sinkTopic = "dwm_unique_visit";
//       3.2 获取kafkasource
        FlinkKafkaConsumer<String> getkafkaSource = MykafkaUtil.getkafkaSource(topic, groupId);
        SingleOutputStreamOperator<JSONObject> jsonobjDS = env.addSource(getkafkaSource)
                .map(r -> JSON.parseObject(r));
// TODO: 按照设备id进行分组
        KeyedStream<JSONObject, String> keyedDS = jsonobjDS.keyBy(jsonobj -> jsonobj.getJSONObject("common").getString("mid"));
        // TODO: 5. 对当前设备的访问情况进行过滤 UV
        SingleOutputStreamOperator<JSONObject> filterDS = keyedDS.filter(
                new RichFilterFunction<JSONObject>() {
                    private ValueState<String> lastVisitDateState;
                    private SimpleDateFormat sdf;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        sdf = new SimpleDateFormat("yyyyMMdd");
                        //创建状态描述器
                        ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<String>("lastVisitDateState", String.class);
                        // 状态存活时间
                        StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(Time.days(1L))
                                // 设置状态修改后,失效时间是否会被修改 默认值
                                .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
                                // 设置状态过期后，如果获取状态是否返回,默认值
                                .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
                                .build();
                        // 设置状态存活时间
                        valueStateDescriptor.enableTimeToLive(ttlConfig);
                        // 获取状态
                        lastVisitDateState = getRuntimeContext().getState(valueStateDescriptor);
                    }


                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {
                        //获取上级页面ID 本页由其他页面跳转进来
                        String lastPageId = jsonObject.getJSONObject("page").getString("last_page_id");
                        //判断是否有上级访问页面 如果有上级访问页面 直接将该日志过滤掉
                        if (lastPageId != null && lastPageId.length() > 0) {
                            return false;
                        }
                        // 获取状态的值 -- 上次访问日期
                        String lastVisitDate = lastVisitDateState.value();
                        // 获取页面日志当前访问日期
                        Long ts = jsonObject.getLong("ts");
                        String curDate = sdf.format(ts);
                        //本日已经访问过
                        if (lastVisitDate != null && lastVisitDate.length() > 0 && lastVisitDate.equals(curDate)) {
                            return false;
                        } else {
                            lastVisitDateState.update(curDate);
                            return true;
                        }

                    }
                }
        );

        filterDS.print(">>>>>");
        // 将uv写会kafka的dwm层
        filterDS.map(jsonobj -> jsonobj.toJSONString())
                .addSink(MykafkaUtil.getKafkaSink(sinkTopic));
        env.execute();

    }
}
